package com.wsc.wisdomchina.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.stx.xhb.mylibrary.XBanner;
import com.umeng.analytics.MobclickAgent;
import com.wsc.wisdomchina.DaoUtils.IndexNameSortDaoUtils;
import com.wsc.wisdomchina.Entity.IndexNameSort;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.EditFunction.IndexEditFunctionActivity;
import com.wsc.wisdomchina.activitity.HomeActivity;
import com.wsc.wisdomchina.activitity.InfoActivity.MoreInfoActivity;
import com.wsc.wisdomchina.activitity.InfoDetailActivity;
import com.wsc.wisdomchina.activitity.JieDaoTeSheActivity;
import com.wsc.wisdomchina.activitity.JuWeiXinXiActivity;
import com.wsc.wisdomchina.activitity.LifeServerActivity;
import com.wsc.wisdomchina.activitity.LivingExpensesActivity;
import com.wsc.wisdomchina.activitity.Login_RegisterActivity;
import com.wsc.wisdomchina.activitity.MessageActivity;
import com.wsc.wisdomchina.activitity.ModuleTabListActivity;
import com.wsc.wisdomchina.activitity.PageInfoDetailActivity;
import com.wsc.wisdomchina.activitity.PeripheralSearchActivity;
import com.wsc.wisdomchina.activitity.ReceptionCenterActivity;
import com.wsc.wisdomchina.activitity.ShangjiaCuXiaoActivity;
import com.wsc.wisdomchina.activitity.fourmodule.CompanyInfoActivity;
import com.wsc.wisdomchina.activitity.fourmodule.FloorEventActivity;
import com.wsc.wisdomchina.activitity.fourmodule.FloorInfoActivity;
import com.wsc.wisdomchina.activitity.fourmodule.HelpOldManActivity;
import com.wsc.wisdomchina.activitity.fourmodule.ModuleEventActivity;
import com.wsc.wisdomchina.activitity.fourmodule.PeopleControlActivity;
import com.wsc.wisdomchina.activitity.fourmodule.PeopleControlPublishActivity;
import com.wsc.wisdomchina.activitity.introduction.BriefIntroductionActivity;
import com.wsc.wisdomchina.activitity.samemodule.InfoAndActivityListActivity;
import com.wsc.wisdomchina.activitity.samemodule.InfoAndListActivity;
import com.wsc.wisdomchina.activitity.wisdomParty.PartyActivity;
import com.wsc.wisdomchina.activitity.wisdomParty.PartyLoginActivity;
import com.wsc.wisdomchina.adapter.FourModuleNewAdapter;
import com.wsc.wisdomchina.adapter.IndexBaiShiTongAdapter;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ClickIntoFragment;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.education.activity.EducationActivity;
import com.wsc.wisdomchina.houselease.activity.HouseLeaseActivity;
import com.wsc.wisdomchina.javaBean.HomeBanner;
import com.wsc.wisdomchina.javaBean.LookHomeNews;
import com.wsc.wisdomchina.javaBean.MyShowItemListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.RespondObj;
import com.wsc.wisdomchina.javaBean.StreetInfoListBean;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyUserBean;
import com.wsc.wisdomchina.jobhunt.activity.JobHuntActivity;
import com.wsc.wisdomchina.notice.activity.AnnouncementListActivity;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyListView;
import com.wsc.wisdomchina.view.UPMarqueeView;
import com.wsc.wisdomchina.view.iphoneDialogBuilder;
import com.wsc.wisdomchina.welfare.activity.WelfareEventActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * 首页
 */
public class HomeFragment extends Fragment implements XBanner.XBannerAdapter, View.OnClickListener, ContributorView {

    List<HomeBanner> homeBannerList = new ArrayList<>();
    private XBanner fragment_first_bannera;//轮播控件
    private PullToRefreshScrollView fragment_first_scrollView;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private final String HOME_BANNER = "HOME_BANNER";
    private final String FOUR_MODULEFLAG = "FOUR_MODULEFLAG";
    private final String HOME_NEWS = "HOME_NEWS";
    private GridView fragment_first_head_sida_gongnneg;
    private int skip = 0;
    private int limit = 6;
    private List<LookHomeNews> homeNewsList;
    private MyApplication myApplication;
    private ClickIntoFragment mCallBack;// 接口变量
    private boolean ishide = false;
    private List<MyShowItemListBean> homeModuleFourList;
    private TextView tv_headtitle;
    private StreetInfoListBean streetInfoListBean;
    private final String LOCATION_FLAG="LOCATION_FLAG";
    private String cidNumber;
    private String cidTitle;
    private LinearLayout layout;
    private UPMarqueeView upview1;
    private List<View> views = new ArrayList<>();
    private TextView tv_more;
    private MyListView mylistview;
    private ImageView iv_message;
    private RelativeLayout layout_baishitong;
    private RelativeLayout layout_linlifuwu;
    private TextView tv_baishitong;
    private TextView tv_linlifuwu;
    private View view_baishitong;
    private View view_linlifuwu;
    private int type = 2;
    private final String BAISHITONGFLAG="BAISHITONGFLAG";
    private final String LINLIFUWUFLAG="LINLIFUWUFLAG";
    private IndexNameSortDaoUtils indexNameSortDaoUtils;
    private IndexBaiShiTongAdapter mBaiShiTongAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        context = getActivity();
        indexNameSortDaoUtils=new IndexNameSortDaoUtils(context);
        String locationString = Tools.getSharedPreferencesStrings(Constant.SharePreferenceLocation, "", context);
        if (locationString.length() > 0) {
            streetInfoListBean = JsonTools.jsonStringToObject(locationString, StreetInfoListBean.class);
            cidNumber=streetInfoListBean.getId();
            cidTitle=streetInfoListBean.getStreetName();
        }
        myApplication = (MyApplication) getActivity().getApplication();
        initView(view);
        contributorPresenter = new ContributorPresenter(this, context);
        getBannerData();
        return view;
    }

    private void initView(View view) {
        mylistview=(MyListView) view.findViewById(R.id.mylistview);

        layout_baishitong=(RelativeLayout) view.findViewById(R.id.layout_baishitong);
        layout_linlifuwu=(RelativeLayout) view.findViewById(R.id.layout_linlifuwu);

        tv_baishitong=(TextView) view.findViewById(R.id.tv_baishitong);
        tv_linlifuwu=(TextView) view.findViewById(R.id.tv_linlifuwu);

        view_baishitong=view.findViewById(R.id.view_baishitong);
        view_linlifuwu=view.findViewById(R.id.view_linlifuwu);

        layout_baishitong.setOnClickListener(this);
        layout_linlifuwu.setOnClickListener(this);

        tv_more=(TextView) view.findViewById(R.id.tv_more);
        tv_more.setOnClickListener(this);
        upview1 = (UPMarqueeView) view.findViewById(R.id.upmv_news);

        tv_headtitle = (TextView) view.findViewById(R.id.tv_headtitle);
        tv_headtitle.setOnClickListener(this);
        if (cidTitle != null) {
            tv_headtitle.setText(cidTitle);
        }

        iv_message=(ImageView) view.findViewById(R.id.iv_message);
        iv_message.setOnClickListener(this);

        fragment_first_bannera = (XBanner) view.findViewById(R.id.fragment_first_bannera);
        fragment_first_scrollView = (PullToRefreshScrollView) view.findViewById(R.id.fragment_first_scrollView);
        fragment_first_scrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        fragment_first_scrollView.requestFocus();
        fragment_first_scrollView.setFocusable(true);
        fragment_first_scrollView.setFocusableInTouchMode(true);

        fragment_first_head_sida_gongnneg = (GridView) view.findViewById(R.id.fragment_first_head_sida_gongnneg);
        Tools.showScrollRefreshView(fragment_first_scrollView);
        //轮播图点击事件
        fragment_first_bannera.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, int position) {
                Intent intent = new Intent(context, InfoDetailActivity.class);
                intent.putExtra("id", homeBannerList.get(position).getId());
                intent.putExtra("flag","BannerDetailFlag");
                context.startActivity(intent);
            }
        });
        fragment_first_scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
                    Tools.showScrollRefreshView(fragment_first_scrollView);
                    getBannerData();
                }
            }
        });

        fragment_first_head_sida_gongnneg.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                if(!isLogin){
                    Toast.makeText(getActivity(),"该功能需要登陆后操作",Toast.LENGTH_LONG).show();
                  return true;
                }
                boolean isFirstLongClick=Tools.getSharedPreferencesboolen(Constant.SharePreferenceFirstLongClick,true,context);
                if(isFirstLongClick)
                {
                    mCallBack.clickIntoFragment();
                }else {
                    //进入功能排序，功能编辑页面
                    Intent intent=new Intent(context, IndexEditFunctionActivity.class);
                    intent.putExtra("cid",cidNumber);
                    startActivityForResult(intent,100);
                }
                return true;
            }
        });

        //四大模快ItemClick点击事件
        fragment_first_head_sida_gongnneg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    final MyShowItemListBean myShowItemListBean=homeModuleFourList.get(position);
                    MyShowItemListBean.MenuserverBean menuserverBean=myShowItemListBean.getMenuserver();
                    if(menuserverBean!=null)
                    {
                        String menuId=myShowItemListBean.getMenuId();
                        if(menuId.equals("-1"))
                        {
                            boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                            if(!isLogin){
                                Toast.makeText(getActivity(),"该功能需要登陆后操作",Toast.LENGTH_LONG).show();
                                return;
                            }
                            boolean isFirstLongClick=Tools.getSharedPreferencesboolen(Constant.SharePreferenceFirstLongClick,true,context);
                            if(isFirstLongClick)
                            {
                                mCallBack.clickIntoFragment();
                            }else {
                                //进入功能排序，功能编辑页面
                                Intent intent=new Intent(context, IndexEditFunctionActivity.class);
                                intent.putExtra("cid",cidNumber);
                                startActivityForResult(intent,100);
                            }
                        }else {
                            String isLast = menuserverBean.getIsLast();
                            String templetType = menuserverBean.getTempletType();
                            if("0".equals(isLast)){
                                Intent intent = new Intent(context, ModuleTabListActivity.class);
                                intent.putExtra("menuId", menuId);
                                intent.putExtra("id", cidNumber);
                                intent.putExtra("title", menuserverBean.getName());
                                intent.putExtra("isLast",isLast);
                                startActivity(intent);
                                return;
                            }else if("1".equals(isLast)){
                                if("0".equals(templetType)){
                                    Intent intent = new Intent(context, ModuleTabListActivity.class);
                                    intent.putExtra("menuId", menuId);
                                    intent.putExtra("id", cidNumber);
                                    intent.putExtra("isLast",isLast);
                                    intent.putExtra("title", menuserverBean.getName());
                                    startActivity(intent);
                                    return;
                                }else if("1".equals(templetType)){
                                    Intent intent = new Intent(context, ModuleEventActivity.class);
                                    intent.putExtra("menuId", menuId);
                                    intent.putExtra("id", cidNumber);
                                    intent.putExtra("isLast",isLast);
                                    intent.putExtra("title", menuserverBean.getName());
                                    startActivity(intent);
                                    return;
                                }
                            }
                            if(menuId.equals("0"))
                            {
                                //生活缴费
                                Intent intent = new Intent(context, LivingExpensesActivity.class);
                                intent.putExtra("name", menuserverBean.getName());
                                intent.putExtra("cid",cidNumber);
                                context.startActivity(intent);
                            }else if(menuId.equals("1"))
                            {
                                // 百姓监督
                                Intent intent = new Intent(context, PeopleControlActivity.class);
                                intent.putExtra("name", menuserverBean.getName());
                                intent.putExtra("cid", cidNumber);
                                context.startActivity(intent);
                            }else
                            {
                                int specialType=menuserverBean.getSpecialType();
                                String templeType=menuserverBean.getTempletType();
                                String name=menuserverBean.getName();
                                if(specialType==0 ||specialType==3 ||specialType==5)
                                {
                                    String shortName=menuserverBean.getShortName();
                                    //普通菜单
                                    //                        else if(nameModule.equals("关心青少年"))
                                    //                        {
                                    //                            Intent intent=new Intent(context, CareTeenagersActivity.class);
                                    //                            intent.putExtra("cid",cidNumber);
                                    //                            startActivity(intent);
                                    //                        }
                                    if (shortName.equals("shjf")) {
                                        //生活缴费
                                        Intent intent = new Intent(context, LivingExpensesActivity.class);
                                        intent.putExtra("name", name);
                                        intent.putExtra("cid", cidNumber);
                                        context.startActivity(intent);
                                    }else if (shortName.equals("sjcx")) {
                                        //商家促销
                                        Intent intent = new Intent(context, ShangjiaCuXiaoActivity.class);
                                        intent.putExtra("id", cidNumber);
                                        intent.putExtra("name", name);
                                        context.startActivity(intent);
                                    } else if (shortName.equals("lyxx")) {
                                        //楼宇信息
                                        Intent intent = new Intent(context, FloorInfoActivity.class);
                                        intent.putExtra("name", "楼宇搜索");
                                        intent.putExtra("id", cidNumber);
                                        context.startActivity(intent);
                                    } else if (shortName.equals("qzzp")) {//求职招聘
                                        Intent intent = new Intent(context, JobHuntActivity.class);
                                        intent.putExtra("name", "求职招聘");
                                        intent.putExtra("id", cidNumber);
                                        context.startActivity(intent);
                                    } else if (shortName.equals("fwzl")) {//房屋租赁
                                        Intent intent = new Intent(context, HouseLeaseActivity.class);
                                        intent.putExtra("name", "房屋租赁");
                                        intent.putExtra("id", cidNumber);
                                        context.startActivity(intent);
                                    } else if (shortName.equals("jypx")) {//教育培训
                                        Intent intent = new Intent(context, EducationActivity.class);
                                        intent.putExtra("name", "教育培训");
                                        intent.putExtra("id", cidNumber);
                                        context.startActivity(intent);
                                    }else if (shortName.equals("qyxx")) {
                                        //企业信息
                                        Intent intent = new Intent(context, CompanyInfoActivity.class);
                                        intent.putExtra("name", "企业搜索");
                                        intent.putExtra("id", cidNumber);
                                        context.startActivity(intent);
                                    }else if (shortName.equals("bsck")) {
                                        //受理中心
                                        Intent intent = new Intent(context, ReceptionCenterActivity.class);
                                        intent.putExtra("id", cidNumber);
                                        intent.putExtra("name", name);
                                        context.startActivity(intent);
                                    }
                                    else if (shortName.equals("zlfw")) {
                                        //助老服务
                                        Intent intent = new Intent(context, HelpOldManActivity.class);
                                        intent.putExtra("name", name);
                                        intent.putExtra("cid", cidNumber);
                                        intent.putExtra("idmodule", menuId);
                                        context.startActivity(intent);
                                    } else if (shortName.equals("zcfw")) {
                                        //助残服务
                                        Intent intent = new Intent(context, HelpOldManActivity.class);
                                        intent.putExtra("name", name);
                                        intent.putExtra("cid", cidNumber);
                                        intent.putExtra("idmodule", menuId);
                                        context.startActivity(intent);
                                    } else if (shortName.equals("bxjd")) {
                                        // 百姓监督
                                        Intent intent = new Intent(context, PeopleControlActivity.class);
                                        intent.putExtra("name", name);
                                        intent.putExtra("cid", cidNumber);
                                        context.startActivity(intent);
                                    }
//                                  else if (name.equals("智慧党建")) {
//                                        String jsonMessage = Tools.getSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo, "", context);
//                                        if (jsonMessage.equals("")) {
//                                            // 智慧党建
//                                            Intent intent = new Intent(context, PartyLoginActivity.class);
//                                            intent.putExtra("name", name);
//                                            intent.putExtra("cid", cidnumber);
//                                            context.startActivity(intent);
//                                        } else {
//                                            PartyUserBean partyUserBean = JsonTools.jsonStringToObject(jsonMessage, PartyUserBean.class);
//                                            Intent intent = new Intent(context, PartyActivity.class);
//                                            intent.putExtra("pId", partyUserBean.getId());
//                                            intent.putExtra("cId", cidnumber);
//                                            context.startActivity(intent);
//                                        }
//                                    }

                                }else if(specialType==1)
                                {
                                    //资讯、活动类菜单
                                    if(isLast.equals("0"))
                                    {
                                        //根据父id查询子级
                                        Intent intent=new Intent(context, InfoAndActivityListActivity.class);
                                        intent.putExtra("cid",cidNumber);
                                        intent.putExtra("menuId",myShowItemListBean.getMenuId());
                                        intent.putExtra("name",menuserverBean.getName());
                                        context.startActivity(intent);
                                    }else
                                    {
                                        //是最终菜单，无子菜单
                                        if(templeType.equals("0"))
                                        {
                                            //资讯
                                            //1,获取街道政务列表
                                            //2,获取街道政务详情
                                            Intent intent=new Intent(context, InfoAndListActivity.class);
                                            intent.putExtra("streetId",cidNumber);
                                            intent.putExtra("menuId",myShowItemListBean.getMenuId());
                                            intent.putExtra("isInfo",true);
                                            intent.putExtra("name",menuserverBean.getName());
                                            context.startActivity(intent);
                                        }else {
                                            //活动
                                            //1,获取街道活动列表
                                            //2,获取活动详情
                                            Intent intent=new Intent(context, InfoAndListActivity.class);
                                            intent.putExtra("streetId",cidNumber);
                                            intent.putExtra("menuId",myShowItemListBean.getMenuId());
                                            intent.putExtra("isInfo",false);
                                            intent.putExtra("name",menuserverBean.getName());
                                            context.startActivity(intent);
                                        }
                                    }
                                }else if(specialType==4)
                                {
                                    //资讯管理
                                    Intent intent1=new Intent(context, MoreInfoActivity.class);
                                    context.startActivity(intent1);
                                }
                            }
                        }
                    }else{
                        boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                        if(!isLogin){
                            Intent intent = new Intent(context, Login_RegisterActivity.class);
                            myApplication.setSourceIntoLoginRegisterActivity(HomeActivity.class.getSimpleName());
                            startActivity(intent);
                            return;
                        }
                        boolean isFirstLongClick=Tools.getSharedPreferencesboolen(Constant.SharePreferenceFirstLongClick,true,context);
                        if(isFirstLongClick)
                        {
                            mCallBack.clickIntoFragment();
                        }else {
                            //进入功能排序，功能编辑页面
                            Intent intent=new Intent(context, IndexEditFunctionActivity.class);
                            intent.putExtra("cid",cidNumber);
                            startActivityForResult(intent,100);
                        }
                    }

                }catch (Exception e)
                {

                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
            String userid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
            contributorPresenter.getAppManageShowItem(cidNumber,userid,1, FOUR_MODULEFLAG);
        }

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mCallBack = (ClickIntoFragment) context;
    }

    //处理Banner图片
    private void getBannerData() {

        if(streetInfoListBean==null)
        {
            String city= myApplication.getLocationCity();
            String country=myApplication.getLocationDistrict();
            contributorPresenter.getHomeFetchStreet(city,country,LOCATION_FLAG);
        }

        if(cidNumber!=null)
        {
            contributorPresenter.getHomeBanner(cidNumber, HOME_BANNER);
            boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
            if(isLogin)
            {
                String userid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                //四大功能
                Log.i("123456789","oncreate 查询：" + cidNumber + "-userid:" + userid);
                contributorPresenter.getAppManageShowItem(cidNumber,userid,1, FOUR_MODULEFLAG);
            }else
            {//不应该读取本地，未登录就使用服务器的默认配置
/*                //四大功能
                IndexNameSort indexNameSort=indexNameSortDaoUtils.queryMeiziById(Constant.daoLocal);
                if(indexNameSort!=null) {
                    String message = indexNameSort.getObjMessage();
                    homeModuleFourList = JsonTools.jsonStringToLinkList(message, MyShowItemListBean.class);
                    if (homeModuleFourList != null && homeModuleFourList.size() > 0) {
                        FourModuleNewAdapter siDaGongNengAdapter = new FourModuleNewAdapter(context, homeModuleFourList);
                        fragment_first_head_sida_gongnneg.setAdapter(siDaGongNengAdapter);
                    }
                }else {
                    contributorPresenter.getAppManageShowItem(cidNumber,null,1, FOUR_MODULEFLAG);
                }*/
                contributorPresenter.getAppManageShowItem(cidNumber,null,1, FOUR_MODULEFLAG);
            }

            //首页资讯
            contributorPresenter.getLookList(cidNumber, "0", skip, limit, HOME_NEWS);
            if(type==2)
            {
                //政务百事通
                contributorPresenter.getAppManageShowItem(cidNumber,null,type,BAISHITONGFLAG);
            }else {
                //邻里服务
                contributorPresenter.getAppManageShowItem(cidNumber,null,type,LINLIFUWUFLAG);
            }

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_headtitle:
/*                Intent intent=new Intent(context, PageInfoDetailActivity.class);
                intent.putExtra("id","3704218965");
                intent.putExtra("name","街道简介");
                startActivity(intent);*/
                break;

            case R.id.tv_more:
                Intent intent1=new Intent(context, MoreInfoActivity.class);
                startActivity(intent1);
                break;

            case R.id.iv_message:
                boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                if(!isLogin){
                    Intent intent = new Intent(context, Login_RegisterActivity.class);
                    myApplication.setSourceIntoLoginRegisterActivity(HomeActivity.class.getSimpleName());
                    startActivity(intent);
                    return;
                }
                Intent message_intent=new Intent(context,MessageActivity.class);
                startActivity(message_intent);
                break;

            case R.id.layout_baishitong:
                layout_baishitong.setBackgroundResource(R.mipmap.bgtab);
                layout_linlifuwu.setBackgroundColor(Color.TRANSPARENT);
                tv_baishitong.setTextColor(getResources().getColor(R.color.OK_color));
                tv_linlifuwu.setTextColor(getResources().getColor(R.color.text_color_one));
                view_baishitong.setVisibility(View.VISIBLE);
                view_linlifuwu.setVisibility(View.GONE);
                type=2;
                if(cidNumber!=null)
                    contributorPresenter.getAppManageShowItem(cidNumber,null,type,BAISHITONGFLAG);
                break;
            case R.id.layout_linlifuwu:
                layout_linlifuwu.setBackgroundResource(R.mipmap.bgtab);
                layout_baishitong.setBackgroundColor(Color.TRANSPARENT);
                tv_linlifuwu.setTextColor(getResources().getColor(R.color.OK_color));
                tv_baishitong.setTextColor(getResources().getColor(R.color.text_color_one));
                view_linlifuwu.setVisibility(View.VISIBLE);
                view_baishitong.setVisibility(View.GONE);
                type=3;
                if(cidNumber!=null)
                    contributorPresenter.getAppManageShowItem(cidNumber,null,type,LINLIFUWUFLAG);
                break;

        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //banner
            if (flag.equals(HOME_BANNER)) {

                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    homeBannerList = JsonTools.jsonStringToList(jsonMessage, HomeBanner.class);
                    List<String> titleList = new ArrayList<>();
                    List<String> purlList = new ArrayList<>();
                    for (int i = 0; i < homeBannerList.size(); i++) {
                        purlList.add(homeBannerList.get(i).getImg());
                        titleList.add(homeBannerList.get(i).getTitle());
                    }
                    fragment_first_bannera.setData(purlList, titleList);
                    fragment_first_bannera.setPoinstPosition(XBanner.RIGHT);//指示器居于右侧
                    fragment_first_bannera.setmAdapter(this);
                }

            } else if (flag.equals(FOUR_MODULEFLAG)) {
                Log.i("123456789",jsonString);
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    homeModuleFourList = JsonTools.jsonStringToList(jsonMessage, MyShowItemListBean.class);
                    if (homeModuleFourList != null && homeModuleFourList.size() > 0) {
                        FourModuleNewAdapter siDaGongNengAdapter = new FourModuleNewAdapter(context, homeModuleFourList);
                        fragment_first_head_sida_gongnneg.setAdapter(siDaGongNengAdapter);

                        boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                        if(isLogin)
                        {
                            String userid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                            IndexNameSort indexNameSort=indexNameSortDaoUtils.queryMeiziById(Constant.daoServer);
                            if(indexNameSort!=null)
                            {
                                indexNameSortDaoUtils.updateIndexNameSort(new IndexNameSort(Constant.daoServer,userid,jsonMessage));
                            }else {
                                indexNameSortDaoUtils.insertIndexNameSort(new IndexNameSort(Constant.daoServer,userid,jsonMessage));
                            }
                        }else
                        {
                            IndexNameSort indexNameSort=indexNameSortDaoUtils.queryMeiziById(Constant.daoLocal);
                            if(indexNameSort!=null)
                            {
                                indexNameSortDaoUtils.updateIndexNameSort(new IndexNameSort(Constant.daoLocal,cidNumber,jsonMessage));
                            }else {
                                indexNameSortDaoUtils.insertIndexNameSort(new IndexNameSort(Constant.daoLocal,cidNumber,jsonMessage));
                            }
                        }
                    }
                }
            }
            else if (flag.equals(HOME_NEWS)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    homeNewsList= JsonTools.jsonStringToList(jsonMessage, LookHomeNews.class);
                    if (homeNewsList != null && homeNewsList.size() > 0) {
                        setView();
                        upview1.setViews(views);
                    }
                }
            }else if(flag.equals(LOCATION_FLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    String message=JsonTools.objectToJsonString(respObj.getMessage());
                    List<StreetInfoListBean> listData=JsonTools.jsonStringToList(message,StreetInfoListBean.class);
                    if(listData!=null &&listData.size()>0)
                    {
                        Tools.setSharedPreferencesStrings(Constant.SharePreferenceLocation,JsonTools.objectToJsonString(listData.get(0)),context);
                        streetInfoListBean=listData.get(0);
                        cidNumber=streetInfoListBean.getId();
                        cidTitle=streetInfoListBean.getStreetName();
                        tv_headtitle.setText(cidTitle);
                        contributorPresenter.getHomeBanner(cidNumber, HOME_BANNER);
                        boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                        if(isLogin)
                        {
                            String userid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                            //四大功能
                            contributorPresenter.getAppManageShowItem(cidNumber,userid,1, FOUR_MODULEFLAG);
                        }else
                        {
                            //四大功能
                            IndexNameSort indexNameSort=indexNameSortDaoUtils.queryMeiziById(Constant.daoLocal);
                            if(indexNameSort!=null) {
                                String messageString = indexNameSort.getObjMessage();
                                homeModuleFourList = JsonTools.jsonStringToLinkList(messageString, MyShowItemListBean.class);
                                if (homeModuleFourList != null && homeModuleFourList.size() > 0) {
                                    FourModuleNewAdapter siDaGongNengAdapter = new FourModuleNewAdapter(context, homeModuleFourList);
                                    fragment_first_head_sida_gongnneg.setAdapter(siDaGongNengAdapter);
                                }
                            }else {
                                contributorPresenter.getAppManageShowItem(cidNumber,null,1, FOUR_MODULEFLAG);
                            }
                        }
                        //首页资讯
                        contributorPresenter.getLookList(cidNumber, "0", skip, limit, HOME_NEWS);
                        if(type==2)
                        {
                            //政务百事通
                            contributorPresenter.getAppManageShowItem(cidNumber,null,type,BAISHITONGFLAG);
                        }else {
                            //邻里服务
                            contributorPresenter.getAppManageShowItem(cidNumber,null,type,LINLIFUWUFLAG);
                        }
                    }
                }
            }
            //百事通
            else if (flag.equals(BAISHITONGFLAG)) {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<MyShowItemListBean> listOne = JsonTools.jsonStringToList(jsonMessage, MyShowItemListBean.class);
                    if (listOne != null && listOne.size() > 0) {
                        mBaiShiTongAdapter = new IndexBaiShiTongAdapter(context,listOne,cidNumber);
                        mylistview.setAdapter(mBaiShiTongAdapter);
                    }
                }
            }
            //邻里服务
            else if (flag.equals(LINLIFUWUFLAG)) {
                RespondObj respObj = JsonTools.jsonStringToObject(jsonString, RespondObj.class);
                String code = respObj.getCode();
                Object object = respObj.getObj();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<MyShowItemListBean> listOne = JsonTools.jsonStringToList(jsonMessage, MyShowItemListBean.class);
                    if (listOne != null && listOne.size() > 0) {
                        mBaiShiTongAdapter = new IndexBaiShiTongAdapter(context,listOne,cidNumber);
                        mylistview.setAdapter(mBaiShiTongAdapter);
                    }
                }
            }

        } catch (Exception e) {

        }
    }

    private void setView() {
        final int length=homeNewsList.size();
        for (int i = 0; i < length; i = i + 2) {
            //设置滚动的单个布局
            LinearLayout moreView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.upview_item, null);
            //初始化布局的控件
            TextView tv1 = (TextView) moreView.findViewById(R.id.tv1);
            TextView tv2 = (TextView) moreView.findViewById(R.id.tv2);
            final LookHomeNews lookHomeNewsOne=homeNewsList.get(i);

            RelativeLayout r1=(RelativeLayout) moreView.findViewById(R.id.rl);
            final RelativeLayout rl2=(RelativeLayout) moreView.findViewById(R.id.rl2);
            /**
             * 设置监听
             */
            r1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(lookHomeNewsOne!=null)
                    {
                        Intent intent = new Intent(context, InfoDetailActivity.class);
                        intent.putExtra("id", lookHomeNewsOne.getId());
                        intent.putExtra("flag",SelectedIndustryFragment.class.getSimpleName());
                        intent.putExtra("title",lookHomeNewsOne.getTitle());
                        startActivity(intent);
                    }

                }
            });


            //进行对控件赋值
            tv1.setText(lookHomeNewsOne.getTitle());
            if (length > i + 1) {
                //因为淘宝那儿是两条数据，但是当数据是奇数时就不需要赋值第二个，所以加了一个判断，还应该把第二个布局给隐藏掉
                final LookHomeNews lookHomeNewsTwo=homeNewsList.get(i+1);
                tv2.setText(lookHomeNewsTwo.getTitle());
                rl2.setVisibility(View.VISIBLE);
            } else {
                rl2.setVisibility(View.GONE);
            }

            final int normalposition=i;
            /**
             * 设置监听
             */
            rl2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(rl2.isEnabled())
                    {
                        final LookHomeNews lookHomeNewsTwo=homeNewsList.get(normalposition+1);
                        Intent intent = new Intent(context, InfoDetailActivity.class);
                        intent.putExtra("id", lookHomeNewsTwo.getId());
                        intent.putExtra("flag",SelectedIndustryFragment.class.getSimpleName());
                        intent.putExtra("title",lookHomeNewsTwo.getTitle());
                        startActivity(intent);
                    }
                }
            });
            //添加到循环滚动数组里面去
            views.add(moreView);
        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
        fragment_first_scrollView.onRefreshComplete();
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        ishide = hidden;
/*        if (hidden) {
        } else {
            getBannerData();
        }*/
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(HomeFragment.class.getSimpleName());
    }

    @Override
    public void onResume() {
        super.onResume();
/*        if (ishide) {

        } else {
            getBannerData();
        }*/
        if(getUserVisibleHint()){
            String userid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
            contributorPresenter.getAppManageShowItem(cidNumber,userid,1, FOUR_MODULEFLAG);
        }
        MobclickAgent.onPageStart(HomeFragment.class.getSimpleName());
    }


    //XBanner处理轮播图片，文字
    @Override
    public void loadBanner(XBanner banner, Object model, View view, int position) {
        try {
            Glide.with(context).load(homeBannerList.get(position).getImg())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into((ImageView) view);
        }catch (Exception e)
        {

        }
    }
}
